{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# random sampling\n",
    "import rss\n",
    "import numpy as np\n",
    "import time\n",
    "start_time = time.time()\n",
    "    \n",
    "parameters = {}\n",
    "net_list = []\n",
    "net_list.append({'net_name':'KNN','sizes':[256,256],'dim_cor':[64,64],'mode':'tensor','weights':'distance'})\n",
    "\n",
    "parameters['net_p'] = {'gpu_id':'cpu','net_name':'composition','net_list':net_list}\n",
    "parameters['data_p'] = {'data_shape':(256,256),'random_rate':0.5,\n",
    "                        'pre_full':True,'mask_type':'random','down_sample_rate':2,\n",
    "                        'data_type':'gray_img','data_path':'./data/img/baboon.bmp'}\n",
    "parameters['train_p'] = {'train_epoch':20}\n",
    "parameters['show_p'] = {'show_type':'gray_img','show_content':'original'}\n",
    "parameters['opt_p'] = {'net': {'opt_name': 'Adam', 'lr': 1e-1, 'weight_decay': 0}}\n",
    "rssnet = rss.rssnet(parameters)\n",
    "\n",
    "rssnet.show()\n",
    "rssnet.show_p['show_content'] = 'recovered'\n",
    "rssnet.net.net_list[0].update_neighbor(n_neighbors=8,mask=rssnet.mask.cpu())\n",
    "for i in range(10):\n",
    "    rssnet.train()\n",
    "    if i % 4 == 0:\n",
    "        rssnet.net.net_list[0].update_neighbor(n_neighbors=8,mask=rssnet.mask.cpu(),mode='patch',labda=1,n_components=8)\n",
    "print(time.time()-start_time)\n",
    "rssnet.show()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# Tucker for random sampling\n",
    "import rss\n",
    "import numpy as np\n",
    "\n",
    "    \n",
    "parameters = {}\n",
    "net_list = []\n",
    "net_list.append({'net_name':'KNN','sizes':[256,256],'dim_cor':[256,256],'mode':'tensor'})\n",
    "\n",
    "parameters['net_p'] = {'gpu_id':0,'net_name':'composition','net_list':net_list}\n",
    "parameters['data_p'] = {'data_shape':(256,256),'random_rate':0.5,\n",
    "                        'pre_full':True,'mask_type':'patch','down_sample_rate':2,\n",
    "                        'data_type':'gray_img','data_path':'./data/img/man.bmp'}\n",
    "parameters['train_p'] = {'train_epoch':10000}\n",
    "parameters['show_p'] = {'show_type':'gray_img','show_content':'original'}\n",
    "rssnet = rss.rssnet(parameters)\n",
    "\n",
    "rssnet.show()\n",
    "rssnet.show_p['show_content'] = 'recovered'\n",
    "rssnet.net.net_list[0].update_neighbor(n_neighbors=5,mask=rssnet.mask.cpu())\n",
    "for i in range(10):\n",
    "    rssnet.train()\n",
    "    rssnet.show()\n",
    "    "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "# TF->INR\n",
    "import rss\n",
    "\n",
    "parameters = {}\n",
    "hash_para = {'net_name':'HashEmbedder','finest_resolution':512,'n_levels':2,'n_features_per_level':1,'base_resolution':512}\n",
    "hash_para = {'net_name':'HashEmbedder','finest_resolution':256,'n_levels':16,'n_features_per_level':2,'base_resolution':16}\n",
    "parameters['net_p'] = {'gpu_id':0,'net_name':'HashINR','hash_mode':'vanilla',\n",
    "                       'hash_para':hash_para,\n",
    "                       'inr_para':{'net_name':'MLP','num_layers':1,'dim_hidden':16}}\n",
    "\n",
    "parameters['data_p'] = {'data_shape':(256,256),'random_rate':0.5,\n",
    "                        'pre_full':False,'mask_type':'img','mask_path':'../mask/mask.png',\n",
    "                        'data_path':'../img/Cameraman.jpg','data_type':'gray_img','ymode':'completion',\n",
    "                       'noise_mode':'gaussian','noise_parameter':25}\n",
    "\n",
    "parameters['train_p'] = {'train_epoch':100}\n",
    "\n",
    "parameters['show_p'] = {'show_type':'gray_img','show_content':'original'}\n",
    "\n",
    "parameters['opt_p'] = {'net': {'opt_name': 'Adam', 'lr': 1e-1, 'weight_decay': 0},\n",
    "                       'reg': {'opt_name': 'Adam', 'lr': 1e-3, 'weight_decay': 0},\n",
    "                       'noise': {'opt_name': 'Adam', 'lr': 1e-4, 'weight_decay': 0}}\n",
    "huber_reg = {'reg_name':'MultiReg','reg_list':[{'reg_name':'INRR','coef':1e-2,'n':256,'mode':0,\n",
    "                                                'w0_initial':1.,'lap_k':1,'lap_mode':'Huber',\n",
    "                                                'huber_delta':0.2,'inrr_alpha':1,\n",
    "                                                'nabla_matrix_order_k':1,\n",
    "                                               'inr_parameter':{'dim_in': 1,'dim_out':100,'w0_initial':10}},\n",
    "                                              {'reg_name':'INRR','coef':1e-2,'n':256,'mode':1,\n",
    "                                               'w0_initial':1.,'lap_k':1,'lap_mode':'Huber',\n",
    "                                               'huber_delta':0.2,'inrr_alpha':1,\n",
    "                                               'nabla_matrix_order_k':1,\n",
    "                                              'inr_parameter':{'dim_in': 1,'dim_out':100,'w0_initial':10}}]}\n",
    "air_reg = {'reg_name':'MultiReg','reg_list':[{'reg_name':'AIR','coef':1e-3,'n':256,'mode':0,'lap_mode':'Huber'},\n",
    "                                             {'reg_name':'AIR','coef':1e-3,'n':256,'mode':1,'lap_mode':'Huber'}]}\n",
    "parameters['reg_p'] = huber_reg\n",
    "rssnet = rss.rssnet(parameters)\n",
    "\n",
    "rssnet.show()\n",
    "rssnet.show_p['show_content'] = 'recovered'\n",
    "for i in range(10):\n",
    "    rssnet.train()\n",
    "    rssnet.show()\n",
    " "
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.0"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
